package user

import (
	"api/common"
	"api/pb/user"
	"context"

	"api/internal/svc"
	"api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateInfoLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUpdateInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateInfoLogic {
	return &UpdateInfoLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UpdateInfoLogic) UpdateInfo(req *types.UserInfo) (resp *types.Empty, err error) {
	userID, err := common.GetUserIDFromCtx(l.ctx)
	if err != nil {
		return nil, err
	}

	_, err = l.svcCtx.UserRPC.UpdateUser(l.ctx, &user.UserInfo{
		Id:     userID,
		Name:   req.Name,
		Mobile: req.Mobile,
		Email:  req.Email,
		Avatar: req.Avatar,
	})
	if err != nil {
		return nil, err
	}
	return &types.Empty{}, nil
}
